﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BaseDll;
using Communicate;
using log4net;
using SerialDict;
using MotionIoLib;
using System.Threading;
using UserData;
using System.ComponentModel;
using ABB.Robotics.Controllers;
using ABB.Robotics.Controllers.Discovery;
using ABB.Robotics.Controllers.IOSystemDomain;
using ABB.Robotics.Controllers.RapidDomain;
namespace OtherDevice
{
    [Description("ABB")]
    public class Robot_ABB:SixAxisRobot_Base
    {
        public Robot_ABB()
        {
            controlFrom = new Form_SixAxisRobot();
        }

        public enum SimDO
        {
            DiPowerOn,
            DiPowerOff,
            DiStop,
            DiStart,
        }


        public enum SimDI
        {
            /// <summary>
            /// 上电状态
            /// </summary>
            DoPowerState,
        }

        public bool IsLogin { get; set; } = false;
        private NetworkScanner scanner = null;
        private Controller controller = null;
        private bool b_HomeDone = false;
        Dictionary<string, int> dic_RobotPoint = new Dictionary<string, int>();

        public override int linkEtherNetControl()
        {
            if (scanner == null)
            {
                scanner = new NetworkScanner();
            }

            //对网络进行扫描
            scanner.Scan();

            ControllerInfoCollection controls = scanner.Controllers;
            if (controls.Count == 0)
            {
                MessageBox.Show("未找到机器人");
                return -1;
            }

            ControllerInfo info = controls[0];
            if (info.Availability == Availability.Available)
            {
                if (controller != null)
                {
                    controller.Logoff();
                    controller.Dispose();
                    controller = null;
                    //如果controller不为null，先登出并Dispose
                }
                controller = ControllerFactory.CreateFrom(info);
                controller.Logon(UserInfo.DefaultUser);//登录机器人
                IsLogin = controller.Connected;
                return 0;
            }
            else
            {
                MessageBox.Show("机器人不可用");
                return -1;
            }
        }

        public override bool ExcuteProgram(string str_ProgramName)
        {
            //待续
            return true;
        }
        public override int SetServo(bool b_Statue)
        {
            SetVirtualIO_Din(SimDO.DiPowerOn, 0);
            Thread.Sleep(100);
            SetVirtualIO_Din(SimDO.DiStart, 0);
            Thread.Sleep(100);
            SetVirtualIO_Din(SimDO.DiPowerOn, 1);
            Thread.Sleep(100);
            SetVirtualIO_Din(SimDO.DiStart, 1);

            return 0;
        }
        public override bool ReadServoStatue()
        {
            int value = -1;
            return ReadVirtualIO_Dout(SimDI.DoPowerState);
        }
        public override int ReadRunState()
        {
            return 0;
        }
        public override int ResetAlarm()
        {
            SetVirtualIO_Din(SimDO.DiPowerOn, 1);
            return 1;
        }
        public override int RunStart()
        {
            while (!b_HomeDone) ;
            SetVirtualIO_Din(SimDO.DiStop, 0);
            Thread.Sleep(200);
            SetVirtualIO_Din(SimDO.DiStart, 0);
            Thread.Sleep(200);
            SetVirtualIO_Din(SimDO.DiStart, 1);

            return 0;

        }
        public override int RunStop()
        {
            while (!b_HomeDone) ;
            SetVirtualIO_Din(SimDO.DiStop, 0);
            Thread.Sleep(200);
            SetVirtualIO_Din(SimDO.DiStop, 1);
            return 0;
        }
        public override bool IsConnect()
        {
            return scanner != null;
        }

        private int SetVirtualIO_Din<T>(T ioIndex, int value)
        {
            try
            {
                string result = ioIndex.ToString();
                Signal signal = controller.IOSystem.GetSignal(ioIndex.ToString());
                DigitalSignal sig = (DigitalSignal)signal; //转化为DigitalSignal类型
                if (value == 1)
                {
                    sig.Set();
                }
                else
                {
                    sig.Reset();
                }
                return 0;
            }
            catch (Exception ex)
            {
                string str = ex.Message;
                return -1;
            }

        }
        private bool ReadVirtualIO_Dout<T>(T ioIndex)
        {
            int num = -1;//"-1";
            num.ToString();
            try
            {
                Signal signal = controller.IOSystem.GetSignal(ioIndex.ToString());
                DigitalSignal sig = (DigitalSignal)signal; //转化为DigitalSignal类型
                return Convert.ToInt16(signal.Value) == 1;
            }
            catch (Exception e)
            {
                return false;
            }

        }

    }
}